package spacegame.model;

import java.util.ArrayList;

import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.debug.Debug;
import org.andengine.util.math.MathUtils;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

import spacegame.interfaces.ISpaceObject;
import spacegame.interfaces.ISpaceWorldContainer;
import spacegame.main.MainActivity;
import spacegame.resources.ResourceFactory;
import spacegame.resources.ResourceFactory.PlanetType;

public class SpaceObjectCollection {

	
	private ArrayList<ISpaceObject> m_innerCollection  = new ArrayList<ISpaceObject>();
	private int m_faceCount;
	private ISpaceWorldContainer m_worldContainer;
	private ResourceFactory m_resourceFactory;
	
	
	public SpaceObjectCollection(ISpaceWorldContainer pContainer,ResourceFactory pResorceFactory) {
		m_worldContainer = pContainer;
		m_resourceFactory = pResorceFactory;
	
	}

	
	public void CreateSunObject(final float pX, final float pY) {

		final Sprite face;
		final Body body;

		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1f,
				0.0f, 1f);

		face = new Sprite(pX, pY,
				m_resourceFactory.TryGetPlanetTexture(PlanetType.Sun),
				m_worldContainer.getVertexBufferObjectManager());

		body = PhysicsFactory.createCircleBody(
				this.m_worldContainer.GetCurrentPhysicsWorld(), pX, pY, 172,
				BodyType.DynamicBody, objectFixtureDef);

		this.m_worldContainer.GetCurrentScene().attachChild(face);	
		this.m_worldContainer.GetCurrentPhysicsWorld()
				.registerPhysicsConnector(
						new PhysicsConnector(face, body, true, false));
		
		final ISpaceObject _planet = new PlanetItem(pX, pY, face, body, 30000);
		m_innerCollection.add(_planet);

	}
	
	public void CreatePlanetObject(final float pX, final float pY, final PlanetType type, final float scale) {

		final Sprite face;
		final Body body;
		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1f, 0.0f, 1f);
		face = new Sprite(pX, pY, m_resourceFactory.TryGetPlanetTexture(type), m_worldContainer.getVertexBufferObjectManager());
		face.setScale(scale);
		body = PhysicsFactory.createCircleBody(this.m_worldContainer.GetCurrentPhysicsWorld(), face, BodyType.StaticBody, objectFixtureDef);
		this.m_worldContainer.GetCurrentScene().attachChild(face);	
		this.m_worldContainer.GetCurrentPhysicsWorld()
				.registerPhysicsConnector(
						new PhysicsConnector(face, body, true, false));
		
		final ISpaceObject _planet = new PlanetItem(pX, pY, face, body, 10000);
		m_innerCollection.add(_planet);

	}
	
	
	
	public void CreateSpaceObject(final float pX, final float pY)
	{
		this.m_faceCount++;
		Debug.d("Faces: " + this.m_faceCount);

		final Sprite face;
		final Body body;

		final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef( 1f, 0.3f, 1f);

		if(this.m_faceCount % 4 == 0) {
			face = new Sprite(pX, pY, m_resourceFactory.TryGetPlanetTexture(PlanetType.Sun), m_worldContainer.getVertexBufferObjectManager());
			//face.setScale(MathUtils.random(0.4f, 1f));
			body = PhysicsFactory.createCircleBody(this.m_worldContainer.GetCurrentPhysicsWorld(), pX, pY, 172, BodyType.DynamicBody, objectFixtureDef);
			
		} else if(this.m_faceCount % 4 == 1)  {
			face = new Sprite(pX, pY, m_resourceFactory.TryGetPlanetTexture(PlanetType.Venus), m_worldContainer.getVertexBufferObjectManager());
			face.setScale(MathUtils.random(0.4f, 1f));
			body = PhysicsFactory.createCircleBody(this.m_worldContainer.GetCurrentPhysicsWorld(), face, BodyType.DynamicBody, objectFixtureDef);
		
	} else if(this.m_faceCount % 4 == 2)  {
		face = new Sprite(pX, pY, m_resourceFactory.TryGetPlanetTexture(PlanetType.Mars), m_worldContainer.getVertexBufferObjectManager());
		face.setScale(MathUtils.random(0.4f, 1f));
		body = PhysicsFactory.createCircleBody(this.m_worldContainer.GetCurrentPhysicsWorld(), face, BodyType.DynamicBody, objectFixtureDef);
	
}
		else {
			face = new Sprite(pX, pY, m_resourceFactory.TryGetPlanetTexture(PlanetType.Jupiter), m_worldContainer.getVertexBufferObjectManager());
			face.setScale(MathUtils.random(0.4f, 1f));
			body = PhysicsFactory.createCircleBody (this.m_worldContainer.GetCurrentPhysicsWorld(), face, BodyType.DynamicBody, objectFixtureDef);
		}
		//face.animate(200);

		this.m_worldContainer.GetCurrentScene().attachChild(face);
				
		
		this.m_worldContainer.GetCurrentPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(face, body, true, false));
		final ISpaceObject _planet = new PlanetItem(pX, pY, face, body, 10000);
		m_innerCollection.add(_planet);
		
	}
	
	public ArrayList<ISpaceObject> GetCollection() {
		return m_innerCollection;
	}


	
	
}
